<template>
  <div class="starrocks-monitor-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-background">
        <div class="bg-pattern"></div>
        <div class="bg-gradient"></div>
      </div>
      
      <div class="page-title">
        <div class="title-icon">
          <div class="icon-container">
            <el-icon size="20"><Monitor /></el-icon>
            <div class="icon-glow"></div>
          </div>
        </div>
        <div class="title-content">
          <div class="title-main">
            <h1>StarRocks 监控管理</h1>
            <div class="title-badge">
              <el-icon><Star /></el-icon>
              <span>CLUSTER</span>
            </div>
          </div>
          <p class="page-description">
            <el-icon><DataAnalysis /></el-icon>
            集群配置与重启历史管理
          </p>
          <div class="title-meta">
            <div class="meta-item">
              <el-icon><Setting /></el-icon>
              <span>配置管理</span>
            </div>
            <div class="meta-item">
              <el-icon><Refresh /></el-icon>
              <span>重启历史</span>
            </div>
            <div class="meta-item">
              <el-icon><TrendCharts /></el-icon>
              <span>实时监控</span>
            </div>
          </div>
        </div>
      </div>
      
      <div class="page-stats">
        <div class="stat-card">
          <div class="stat-icon">
            <el-icon><Setting /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.total_configs || 0 }}</div>
            <div class="stat-label">总配置数</div>
            <div class="stat-trend">
              <el-icon><Menu /></el-icon>
              <span>全部</span>
            </div>
          </div>
        </div>
        
        <div class="stat-card running">
          <div class="stat-icon">
            <el-icon><Check /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.running_configs || 0 }}</div>
            <div class="stat-label">运行中</div>
            <div class="stat-trend">
              <el-icon><Check /></el-icon>
              <span>生效中</span>
            </div>
          </div>
        </div>
        
        <div class="stat-card">
          <div class="stat-icon">
            <el-icon><Refresh /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.total_restarts || 0 }}</div>
            <div class="stat-label">总重启数</div>
            <div class="stat-trend">
              <el-icon><TrendCharts /></el-icon>
              <span>历史</span>
            </div>
          </div>
        </div>
        
        <div class="stat-card warning">
          <div class="stat-icon">
            <el-icon><Warning /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-number">{{ stats.recent_restarts || 0 }}</div>
            <div class="stat-label">近7天重启</div>
            <div class="stat-trend">
              <el-icon><Warning /></el-icon>
              <span>需关注</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- Tab 标签页 -->
    <el-tabs v-model="activeTab" class="monitor-tabs" @tab-change="handleTabChange">
      <el-tab-pane label="配置管理" name="config">
        <ConfigManagement ref="configRef" @stats-updated="fetchStats" />
      </el-tab-pane>
      <el-tab-pane label="重启历史" name="history">
        <RestartHistory ref="historyRef" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { 
  Monitor, Star, DataAnalysis, Setting, Refresh, TrendCharts,
  Check, Warning, Menu
} from '@element-plus/icons-vue'
import ConfigManagement from './components/ConfigManagement.vue'
import RestartHistory from './components/RestartHistory.vue'
import api from '@/utils/api'
import { createLogger } from '@/utils/logger'

const componentLogger = createLogger('StarRocksMonitor')

const activeTab = ref('config')
const configRef = ref(null)
const historyRef = ref(null)

const stats = ref({
  total_configs: 0,
  running_configs: 0,
  total_restarts: 0,
  recent_restarts: 0
})

// 获取统计信息
const fetchStats = async () => {
  try {
    const response = await api.get('/starrocks-cluster-monitor/stats')
    if (response.data.success) {
      stats.value = response.data.data
      componentLogger.info('统计数据加载成功', stats.value)
    }
  } catch (error) {
    componentLogger.error('加载统计数据失败', error)
  }
}

// Tab 切换处理
const handleTabChange = (tabName) => {
  componentLogger.info('切换Tab', { tab: tabName })
  // 切换到重启历史时，如果未加载则加载数据
  if (tabName === 'history' && historyRef.value && historyRef.value.fetchData) {
    historyRef.value.fetchData()
  }
}

onMounted(() => {
  fetchStats()
  componentLogger.info('StarRocks监控管理页面加载完成')
})
</script>

<style scoped>
.starrocks-monitor-container {
  padding: 0;  /* 页面容器不设置内边距，距离菜单栏的20px由布局的.content提供 */
  background: var(--el-bg-color-page);
  min-height: 100vh;
}

/* 页面头部 */
.page-header {
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding: 20px 24px;
  border-radius: 12px;
  overflow: hidden;
  color: #303133;
  min-height: 120px;
  border: 1px solid #e4e7ed;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.header-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0.05;
  pointer-events: none;
}

.bg-pattern {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: 
    radial-gradient(circle at 20% 80%, rgba(64, 158, 255, 0.05) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(64, 158, 255, 0.05) 0%, transparent 50%);
  animation: patternMove 30s ease-in-out infinite;
}

.bg-gradient {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
}

.page-title {
  display: flex;
  gap: 16px;
  align-items: center;
  flex: 1;
  position: relative;
  z-index: 1;
}

.title-icon .icon-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #67c23a 0%, #85ce61 100%);
  border-radius: 8px;
  color: white;
  box-shadow: 0 2px 8px rgba(103, 194, 58, 0.2);
  transition: all 0.3s ease;
}

.title-icon .icon-container:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 16px rgba(103, 194, 58, 0.3);
}

.icon-glow {
  position: absolute;
  top: -4px;
  left: -4px;
  right: -4px;
  bottom: -4px;
  background: linear-gradient(45deg, rgba(103, 194, 58, 0.2), rgba(133, 206, 97, 0.3));
  border-radius: 12px;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.title-icon .icon-container:hover .icon-glow {
  opacity: 1;
}

.title-content {
  flex: 1;
}

.title-main {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.title-main h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  letter-spacing: -0.3px;
}

.title-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 12px;
  background: linear-gradient(135deg, #67c23a 0%, #85ce61 100%);
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  color: white;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  box-shadow: 0 2px 8px rgba(103, 194, 58, 0.25);
}

.page-description {
  margin: 0 0 12px 0;
  color: #606266;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.5;
  display: flex;
  align-items: center;
  gap: 8px;
}

.page-description .el-icon {
  font-size: 16px;
  color: #909399;
}

.title-meta {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #909399;
  font-size: 12px;
  font-weight: 500;
}

.meta-item .el-icon {
  font-size: 14px;
}

/* 统计卡片 */
.page-stats {
  display: flex;
  gap: 20px;
  position: relative;
  z-index: 1;
}

.stat-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 8px;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  min-width: 140px;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.stat-icon {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f2f5;
  color: #606266;
  flex-shrink: 0;
}

.stat-card.running .stat-icon {
  background: #f0f9ff;
  color: #67c23a;
}

.stat-card.warning .stat-icon {
  background: #fef0f0;
  color: #f56c6c;
}

.stat-content {
  flex: 1;
  min-width: 0;
}

.stat-number {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 12px;
  color: #909399;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-trend {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: #606266;
}

.stat-trend .el-icon {
  font-size: 12px;
}

/* Tab 样式 */
.monitor-tabs {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.monitor-tabs :deep(.el-tabs__item) {
  font-size: 16px;
  font-weight: 500;
}

.monitor-tabs :deep(.el-tabs__item.is-active) {
  color: #667eea;
}

.monitor-tabs :deep(.el-tabs__active-bar) {
  background-color: #667eea;
}

/* 动画效果 */
@keyframes patternMove {
  0%, 100% {
    transform: translateX(0) translateY(0);
  }
  25% {
    transform: translateX(-10px) translateY(-5px);
  }
  50% {
    transform: translateX(0) translateY(-10px);
  }
  75% {
    transform: translateX(10px) translateY(-5px);
  }
}
</style>

